
#include <wsnos.h>

/************************************************************/
static list_head_t epool_list_head;				

#ifdef OSEL_EPOOL_DEBUG
static osel_uint8_t g_osel_maxepool_cnt = 0;   		
static osel_uint8_t g_osel_epool_cnt = 0;				
#endif
/************************************************************/
osel_bool_t osel_epool_init(osel_uint32_t event_cnts)
{
	event_pool_t *epool = OSEL_NULL;
	osel_uint8_t i =0;

	list_init(&epool_list_head);
	
	epool = (event_pool_t *)osel_mem_alloc(sizeof(event_pool_t) * event_cnts);
	OSEL_ASSERT(epool != OSEL_NULL);

	if(epool == OSEL_NULL)
	{
		return OSEL_FALSE;
	}
	for(i = 0; i< event_cnts; i++)
	{
		list_append(&(epool + i)->list, &epool_list_head); 
	}
	
    return OSEL_TRUE;
}

event_pool_t *osel_epool_malloc(_EPOOL_LINE1_)
{
	event_pool_t *epool = OSEL_NULL;
	OSEL_INT_LOCK();
	epool = list_entry_decap(&epool_list_head, event_pool_t, list);
	OSEL_INT_UNLOCK();

	if(epool != OSEL_NULL)
	{
		osel_memset((event_pool_t *)epool, 0,sizeof(event_pool_t));
		list_init(&(epool->list));
		#ifdef OSEL_EPOOL_DEBUG
		OSEL_INT_LOCK();
		epool->alloc_line = line;

		if(g_osel_maxepool_cnt < ++g_osel_epool_cnt)
		{
			g_osel_maxepool_cnt = g_osel_epool_cnt;
		}
		OSEL_INT_UNLOCK();
		#endif
	}

	return epool;
}

void osel_epool_free(event_pool_t **epool _EPOOL_LINE2_)
{
	OSEL_ASSERT(*epool != OSEL_NULL);
	if(*epool == OSEL_NULL)
	{
		return;
	}

	#ifdef OSEL_EPOOL_DEBUG
	OSEL_INT_LOCK();
	g_osel_epool_cnt--;
	OSEL_INT_UNLOCK();
	#endif

	osel_memset((event_pool_t *)(*epool), 0, sizeof(event_pool_t));

	#ifdef OSEL_EPOOL_DEBUG
	OSEL_INT_LOCK();
	(*epool)->free_line = line;
	OSEL_INT_UNLOCK();
	#endif

	OSEL_INT_LOCK();
	list_append(&((*epool)->list), &epool_list_head);	
	OSEL_INT_UNLOCK();

	*epool = OSEL_NULL;
}


/**@}**/

